package com.ychen.ruijee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ychen.ruijee.bean.ShoppingCart;
import com.ychen.ruijee.common.BaseContext;
import com.ychen.ruijee.common.R;
import com.ychen.ruijee.dao.ShoppingCartDao;
import com.ychen.ruijee.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ychen@gotop.cn
 * @date 2024-06-08 14:25
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao,ShoppingCart> implements ShoppingCartService {
    /**
     * 添加商品到购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart saveShopoingCart(ShoppingCart shoppingCart) {
        log.info("要添加到购物车中的信息为 ：{}",shoppingCart.toString());
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 判断是套餐 还是菜品
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            // 添加到购物车中的是 菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            // 菜品的 口味不一致 也是新商品
            wrapper.eq(shoppingCart.getDishFlavor()!= null ,ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        }else {
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        // 查询当前添加的东西 是否在购物车中
        ShoppingCart cartSomeone = this.getOne(wrapper);
        if (null == cartSomeone){
            // 不存在 保存
            cartSomeone = shoppingCart;
            cartSomeone.setNumber(1);
            cartSomeone.setUserId(userId);
            cartSomeone.setCreateTime(LocalDateTime.now());
        }else{
            // 已经存在 直接更新
            Integer count = cartSomeone.getNumber();
            cartSomeone.setNumber(count + 1);
        }
        boolean flag = this.saveOrUpdate(cartSomeone);
        return flag ? cartSomeone : null;
    }

    /**
     * 减少 购物车中的 份数
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public boolean subShoppingCart(ShoppingCart shoppingCart) {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        // 判断是 菜品还是 菜单
        Long dishId = shoppingCart.getDishId();
        if(dishId == null){
            // 是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else{
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        // 查询 对应的数据库中的记录
        int count = this.count(queryWrapper);
        // 等于1 直接删除 大于1 数量减一
        if(count == 1){
            this.remove(queryWrapper);
        }else if (count > 1){
            // 现根据 queryWrapper 查询
            List<ShoppingCart> shoppingCartList = this.list(queryWrapper);
            shoppingCartList.stream().map((item)->{
                item.setNumber(item.getNumber() -1);
                item.setCreateTime(LocalDateTime.now());
                return item;
            }).collect(Collectors.toList());

            this.updateBatchById(shoppingCartList);
        }
        return true;
    }
}
